﻿using System;
using System.IO;
using System.Reflection;
using System.Collections;
using Reflector;
using Reflector.CodeModel;
using Reflector.Graph.Graphs;
using System.Collections.Generic;

namespace ReflectorAPP
{
    class Program
    {
        static void MainOld(string[] args)
        {
            ArrayList listMethods = new ArrayList();
            try
            {
                Console.WriteLine("\nStart method reference analysis\n");
                //IServiceProvider serviceProvider = new Application(null);
                //IAssemblyLoader assemblyLoader = (IAssemblyLoader)
                //    serviceProvider.GetService(typeof(IAssemblyLoader));
                IServiceProvider serviceProvider = new ApplicationManager(null);
                //ILanguageManager languageManager = (ILanguageManager)serviceProvider.GetService(typeof(ILanguageManager));
                IAssemblyManager assemblyManager = (IAssemblyManager)serviceProvider.GetService(typeof(IAssemblyManager));
                IAssembly rAssembly =
                    assemblyManager.LoadFile("CombinationsLib.dll");
                Console.WriteLine("Assembly: " + rAssembly.ToString());

                foreach (IModule module in rAssembly.Modules)
                {
                    foreach (ITypeDeclaration typeDeclaration in module.Types)
                    {
                        foreach (IMethodDeclaration methodDeclaration in typeDeclaration.Methods)
                        {
                            //Suresh: Adding code for computing graph here
                            ITranslatorManager itmObj = (ITranslatorManager)serviceProvider.GetService(typeof(ITranslatorManager));
                            IMethodDeclaration visitedMethod = itmObj.CreateDisassembler(null, null).TranslateMethodDeclaration(methodDeclaration);
                            //StatementGraphPopulator sgpObj = new StatementGraphPopulator(itmObj);
                            //StatementGraph graphObj = sgpObj.BuildGraphFromMethod(methodDeclaration);
                            //Console.WriteLine(graphObj);
                            //End of code for building the graph

                            Console.WriteLine("Method: " + methodDeclaration);
                            IMethodBody body = methodDeclaration as IMethodBody;
                            if (body != null)
                            {
                                foreach (IInstruction inst in body.Instructions)
                                {
                                    switch (inst.Code.ToString())
                                    {
                                        case "call":
                                            Console.WriteLine("  Calls to: " + inst.Code.ToString());
                                            break;
                                        case "callvirt":
                                            Console.WriteLine("  Calls to: " + inst.Code.ToString());
                                            break;
                                        case "calli":
                                            Console.WriteLine("  Calls to: " + inst.Code.ToString());
                                            break;
                                        case "newobj":
                                            Console.WriteLine("  Calls to: " + inst.Code.ToString());
                                            break;
                                    }
                                }
                            }
                        }
                    }
                }

                assemblyManager.Unload(rAssembly);
                
            }
            catch (Exception ex)
            {
                Console.WriteLine("Fatal error: " + ex.Message);
            }
            Console.WriteLine("\nDone");
            Console.ReadKey();
        }
    }
}


